﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace CRM_Project
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString.Count != 0)
                {
                    Car car = (from Car c in new crmengine().Cars
                               where c.ID == int.Parse(Request.QueryString["VehicleID"])
                               select c).First();

                    dateadded_textbox.Text = car.DateAdded.ToString();
                    carID_textbox.Text = car.ID.ToString();
                    mileage_textbox.Text = car.Mileage.ToString();
                    status_textbox.Text = car.Status.Description;
                    make_textbox.Text = car.Model.Manufacturer.Name;
                    model_textbox.Text = car.Model.Name;
                    color_textbox.Text = car.Color;
                    lot_textbox.Text = car.Lot.Name;
                    regstate_textbox.Text = car.RegState;
                }
            }
        }

        protected void update_button_Click(object sender, EventArgs e)
        {
            crmengine crm = new crmengine();
            Car car = (from Car c in crm.Cars
                       where c.ID == int.Parse(Request.QueryString["VehicleID"])
                       select c).First();
            car.Mileage = int.Parse(mileage_textbox.Text);
            car.Color = color_textbox.Text;
            car.RegState = regstate_textbox.Text;
            crm.SubmitChanges();
        }

        protected void service_button_Click(object sender, EventArgs e)
        {

        }

        protected void repair_button_Click(object sender, EventArgs e)
        {
            crmengine crm = new crmengine();
            RepairRequest rr = new RepairRequest();
            crm.RepairRequests.InsertOnSubmit(rr);
            rr.CarID = int.Parse(Request.QueryString["VehicleID"]);
            rr.Requestee = "Anonymous";
            rr.TimeRequested = DateTime.Now;
            rr.TimeGranted = DateTime.Now;
            rr.Reason = repairdesc_textbox.Text;
            crm.SubmitChanges();
            repairdesc_textbox.Text = "";
        }

        protected void crash_button_Click(object sender, EventArgs e)
        {
            crmengine crm = new crmengine();
            Crash c = new Crash();
            crm.Crashes.InsertOnSubmit(c);
            c.CarID = int.Parse(Request.QueryString["VehicleID"]);
            c.Description = crash_textbox.Text;
            c.Time = DateTime.Now;
            crm.SubmitChanges();
            crash_textbox.Text = "";
        }
    }
}
